package com.quanyan.ic.http.controller;

import com.alibaba.fastjson.JSON;
import com.quanyan.ic.cache.PlaceLightSettingDao;
import com.quanyan.ic.common.http.APIResponse;
import com.quanyan.ic.common.http.enums.ApiResponseEnum;
import com.quanyan.ic.common.paging.PageResult;
import com.quanyan.ic.http.dao.entry.LightAlarmEntry;
import com.quanyan.ic.http.entity.req.ReqQuerySiteOrderStatus;
import com.quanyan.ic.http.entity.req.ReqSendLightCmd;
import com.quanyan.ic.http.service.LightControlService;
import com.quanyan.ic.model.PlaceLightSetting;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by zhuo.shi on 2017/5/31.
 */
@RestController
@RequestMapping("/larkic/placelight")
public class LightControlController {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(LightControlController.class);

    @Autowired
    private LightControlService lightControlService;

    @Autowired
    private PlaceLightSettingDao placeLightSettingDao;

    @RequestMapping(value = "/getPlaceLightSetting")
    APIResponse<?> getPlaceLightSetting(@RequestBody Map<String, Object> map) {
        Integer pid = MapUtils.getInteger(map, "placeId");
        if (null == pid || pid <= 0) {
            return APIResponse.returnFail("场馆id必须大于0");
        }
        return APIResponse.returnSuccess(placeLightSettingDao.getLightControlSetting(pid));
    }

    @RequestMapping(value = "/setPlaceLightSetting")
    APIResponse<?> setPlaceLightSetting(@RequestBody PlaceLightSetting placeLightSetting) {
        lightControlService.setPlaceLightSetting(placeLightSetting);
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/getSiteOrderStatus")
    public APIResponse getSiteOrderStatus(@RequestBody ReqQuerySiteOrderStatus req) {
        if (req.getPlaceId() == null || req.getSiteIds() == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(lightControlService.querySiteOrderStatus(req));
    }

    /**
     * 查看场地灯光异常告警信息列表
     */
    @RequestMapping(value = "/queryLightAlarms")
    APIResponse<?> queryLightAlarms(@RequestBody Map<String, Object> map) {
        logger.info("queryLightAlarms: params={}", map);
        PageResult<List<LightAlarmEntry>> result = lightControlService.queryLightAlarms(map);
        logger.info("return: {}", JSON.toJSONString(result));
        return APIResponse.returnSuccess(result);
    }

    /**
     * 手动开关灯接口
     */
    @RequestMapping(value = "/sendLightCmd")
    APIResponse<?> sendLightCmd(@RequestBody ReqSendLightCmd sendLightCmd) {
        if (sendLightCmd.getPlaceId() == null || sendLightCmd.getSiteIds() == null || sendLightCmd.getLightCmd() == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        logger.info("sendLightCmd: {}", JSON.toJSONString(sendLightCmd));
        lightControlService.sendLightCmd(sendLightCmd);
        return APIResponse.returnSuccess();
    }

}
